package com.example.medical_delivery_system.service.impl;

/**
 * BelongsProject: medical_delivery_system
 * Author: Kotoba Trily
 * CreateTime: 2022/12/25 17:27
 * Description:
 * Version: 1.0
 */

import com.example.medical_delivery_system.common.ServiceResultEnum;
import com.example.medical_delivery_system.dao.*;
import com.example.medical_delivery_system.entity.*;
import com.example.medical_delivery_system.service.*;
import com.example.medical_delivery_system.util.NumberUtil;
import com.example.medical_delivery_system.util.SystemUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class DeliveryLoginServiceImpl implements DeliveryLoginService {

    @Resource
    private DeliveryLoginMapper deliveryLoginMapper;

    @Resource
    private DeliveryTokenMapper deliveryTokenMapper;

    @Override
    public String login(String userName, String password) {
        User loginUser = deliveryLoginMapper.login(userName, password);
        if (loginUser != null) {
            //登录后即执行修改token的操作
            String token = getNewToken(System.currentTimeMillis() + "", loginUser.getUserId());
            UserToken UserToken = deliveryTokenMapper.selectByPrimaryKey(loginUser.getUserId());
            //当前时间
            Date now = new Date();
            //过期时间
            Date expireTime = new Date(now.getTime() + 2 * 24 * 3600 * 1000);//过期时间 48 小时
            if (UserToken == null) {
                UserToken = new UserToken();
                UserToken.setUserId(loginUser.getUserId());
                UserToken.setToken(token);
                UserToken.setUpdateTime(now);
                UserToken.setExpireTime(expireTime);
                //新增一条token数据
                if (deliveryTokenMapper.insertSelective(UserToken) > 0) {
                    //新增成功后返回
                    return token;
                }
            } else {
                UserToken.setToken(token);
                UserToken.setUpdateTime(now);
                UserToken.setExpireTime(expireTime);
                //更新
                if (deliveryTokenMapper.updateByPrimaryKeySelective(UserToken) > 0) {
                    //修改成功后返回
                    return token;
                }
            }

        }
        return ServiceResultEnum.LOGIN_ERROR.getResult();
    }


    /**
     * 获取token值
     *
     * @param timeStr
     * @param userId
     * @return
     */
    private String getNewToken(String timeStr, Long userId) {
        String src = timeStr + userId + NumberUtil.genRandomNum(6);
        return SystemUtil.genToken(src);
    }


    @Override
    public User getUserDetailById(Long loginUserId) {
        return deliveryLoginMapper.selectByPrimaryKey(loginUserId);
    }

    @Override
    public Boolean updatePassword(Long loginUserId, String originalPassword, String newPassword) {
        User User = deliveryLoginMapper.selectByPrimaryKey(loginUserId);
        //当前用户非空才可以进行更改
        if (User != null) {
            //比较原密码是否正确
            if (originalPassword.equals(User.getLoginPassword())) {
                //设置新密码并修改
                User.setLoginPassword(newPassword);
                if (deliveryLoginMapper.updateByPrimaryKeySelective(User) > 0 && deliveryTokenMapper.deleteByPrimaryKey(loginUserId) > 0) {
                    //修改成功且清空当前token则返回true
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public Boolean updateName(Long loginUserId, String loginUserName, String nickName) {
        User User = deliveryLoginMapper.selectByPrimaryKey(loginUserId);
        //当前用户非空才可以进行更改
        if (User != null) {
            //设置新名称并修改
            User.setLoginUserName(loginUserName);
            if (deliveryLoginMapper.updateByPrimaryKeySelective(User) > 0) {
                //修改成功则返回true
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean logout(Long UserId) {
        return deliveryTokenMapper.deleteByPrimaryKey(UserId) > 0;
    }
}
